﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GU.WebERP.Model;
using System.Data;
using GU.WebERP.DAL;
using YGOP.ESB.Log;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Collections;
using JnsFramework.Condition;
using GU.WebERP.Core.Utils;
using GU.WebERP.Core.AjaxFx;

namespace GU.WebERPApp.Ajax.Sys
{
    [AjaxFxClass("itm.sys.usermenu", "用户API")]
    public class UserMenuMethods
    {
        [AjaxFxMethodAttribute("itm.sys.usermenu.get", "获取所有用户")]
        public AjaxFxRspJson GetAll(HttpContext context)
        {
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);
            DAL_Fct_User dal = new DAL_Fct_User();
            try
            {
                var list = dal.GetAll();
                if (list == null) list = new List<Model_Fct_User>();
                ajax.MiniUI.Add("total", 0);
                ajax.MiniUI.Add("data", JToken.FromObject(list));
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "获取所有用户失败!";
                LogManager.WriteAppError(ex);
            }
            return ajax;
        }

        [AjaxFxMethodAttribute("itm.sys.usermenu.getsingle", "获取单个用户")]
        public AjaxFxRspJson GetSingle(HttpContext context)
        {
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);
            DAL_Fct_User dal = new DAL_Fct_User();
            try
            {
                int user_MenuId = Convert.ToInt32(context.Request["umid"]);
                ajax.MiniUI.Add("data", JsonConvert.SerializeObject(dal.GetSingle(user_MenuId)));
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "获取单个用户失败!";
                LogManager.WriteAppError(ex);
            }
            return ajax;
        }

        [AjaxFxMethodAttribute("itm.sys.usermenu.add", "新增用户")]
        public AjaxFxRspJson Add(HttpContext context)
        {
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);
            DAL_Fct_User dal = new DAL_Fct_User();
            try
            {
                Hashtable ht = ConvertHashTable.ToHashTable(HttpUtility.UrlDecode(context.Request["s"]));
                ht["Password"] = MD5Helper.Encrypt(ht["Password"].ToString());
                ht.Remove("UserId");

                if (ht["IsAdmin"].ToString() == "1")
                {
                    ht["MenuRights"] = "";
                }
                int count = dal.Insert(ht);

                if (count > 0)
                {
                    ajax.RspCode = 1;
                    ajax.RspMsg = "新增用户成功!";
                }
                else
                {
                    ajax.RspCode = 0;
                    ajax.RspMsg = "新增用户失败!";
                }
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "新增用户失败!";
                LogManager.WriteAppError(ex);
            }
            return ajax;
        }

        [AjaxFxMethodAttribute("itm.sys.usermenu.update", "修改用户")]
        public AjaxFxRspJson Update(HttpContext context)
        {
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);
            DAL_Fct_User dal = new DAL_Fct_User();
            try
            {
                string uid = string.Empty;
                Hashtable ht = ConvertHashTable.ToHashTable(HttpUtility.UrlDecode(context.Request["s"]));
                uid = ht["UserId"].ToString();
                ht.Remove("UserId");
                if (ht.ContainsKey("Password") && !string.IsNullOrWhiteSpace(ht["Password"].ToString()))
                    ht["Password"] = MD5Helper.Encrypt(ht["Password"].ToString());
                else
                    ht.Remove("Password");

                if (ht["IsAdmin"].ToString() == "1")
                {
                    ht["MenuRights"] = "";
                }

                ConditionBuilder condition = new ConditionBuilder();
                condition.And("UserId").Equal(uid);
                int count = dal.Update(ht, condition);

                if (count > 0)
                {
                    ajax.RspCode = 1;
                    ajax.RspMsg = "新增用户成功!";
                }
                else
                {
                    ajax.RspCode = 0;
                    ajax.RspMsg = "新增用户失败!";
                }
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "修改用户失败!";
                LogManager.WriteAppError(ex);
            }
            return ajax;
        }

        [AjaxFxMethodAttribute("itm.sys.usermenu.delete", "删除用户")]
        public AjaxFxRspJson Delete(HttpContext context)
        {
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);
            DAL_Fct_User dal = new DAL_Fct_User();
            try
            {
                string userid = context.Request["uid"];
                Hashtable ht = new Hashtable();
                ht.Add("Disabled", 1);
                ConditionBuilder condition = new ConditionBuilder();
                condition.And("UserId").Equal(userid);
                int count = dal.Update(ht, condition);
                if (count > 0)
                {
                    ajax.RspCode = 1;
                    ajax.RspMsg = "删除用户成功!";
                }
                else
                {
                    ajax.RspCode = 0;
                    ajax.RspMsg = "删除用户失败!";
                }
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "删除用户失败!";
                LogManager.WriteAppError(ex);
            }
            return ajax;
        }

        [AjaxFxMethodAttribute("itm.sys.usermenu.login", "登入")]
        public AjaxFxRspJson Login(HttpContext context)
        {
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);
            DAL_Fct_User dal = new DAL_Fct_User();
            try
            {
                string name = context.Request["name"];
                string pwd = context.Request["pwd"];
                Model_Fct_User model = dal.Login(name, MD5Helper.Encrypt(pwd));
                if (model != null)
                {
                    DAL_Dim_Menu dal_menu = new DAL_Dim_Menu();
                    if (model.IsAdmin == 1)
                    {
                        var list = dal_menu.GetAll();
                        model.UserMenu = SysMenuMethods.ToUserMenu(list);
                    }
                    else
                    {
                        var list = dal_menu.GetAllByIds(model.MenuRights);
                        model.UserMenu = SysMenuMethods.ToUserMenu(list);
                    }

                    context.Session["User"] = model;
                    ajax.RspCode = 1;
                    ajax.RspMsg = "用户登入成功!";
                }
                else
                {
                    ajax.RspCode = 0;
                    ajax.RspMsg = "用户登入失败!";
                }
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "用户登入失败!";
                LogManager.WriteAppError(ex);
            }
            return ajax;
        }

        [AjaxFxMethodAttribute("itm.sys.usermenu.getcurrentusermenu", "获取当前用户菜单")]
        public AjaxFxRspJson GetCurrentUserMenu(HttpContext context)
        {
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);

            try
            {
                Model_Fct_User currentUser = context.Session["User"] as Model_Fct_User;
                ajax.MiniUIJson = JsonConvert.SerializeObject(currentUser.UserMenu.FindAll(f => f.display == 1));
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "获取当前用户菜单失败!";
                LogManager.WriteAppError(ex);
            }
            return ajax;
        }

        [AjaxFxMethodAttribute("itm.sys.usermenu.loginout", "注销")]
        public AjaxFxRspJson LoginOut(HttpContext context)
        {
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);
            try
            {
                context.Session["User"] = null;
                ajax.RspCode = 1;
                ajax.RspMsg = "注销成功!";
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "注销失败!";
                LogManager.WriteAppError(ex);
            }
            return ajax;
        }
    }
}